﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace DemoGame1
{
    public class Guide : Menu
    {
        int _menuItems;

        KeyboardState oldState;
        Color Normal = Color.Silver;
        Color Hilite = Color.Red;
        MyButton background;
        Player player;
        ContentManager _content;

        private int selectedIndex = 0;

        public Guide(ContentManager content, string strXML)
        {
            _content = content;
            myButtons = new List<MyButton>();
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlElement backGround = (XmlElement)doc.SelectSingleNode("//BackGround");
            //MyButton backGroundButton;
            if (backGround != null)
            {
                background = new MyButton(_content, backGround);
            }
            //add char
            XmlElement buttons = (XmlElement)doc.SelectSingleNode("//Buttons");
            foreach (XmlNode button in buttons.ChildNodes)
            {
                MyButton bt = new MyButton(_content, (XmlElement)button);
                myButtons.Add(bt);
            }
            _menuItems = 6;
            if (myGame.theme == Global.THEMES.KEN_THEME)
            {
                this.player = new Player(_content, "Ken", new Vector2(700, 400), new Vector2(62, 102));
            }
            if (myGame.theme == Global.THEMES.RYU_THEME)
            {
                this.player = new Player(_content, "Ryu", new Vector2(700, 400), new Vector2(62, 102));
            }
        }

        public override void HandleKeyInput()
        {
            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.Back))
            {
                if (!oldState.IsKeyDown(Keys.Back))
                {
                    myGame.currentScreen = CurrentScreen.StartMenu;
                }
            }
            if (newState.IsKeyDown(Keys.Up))
            {
                if (!oldState.IsKeyDown(Keys.Up))
                {
                    player.SetFrameSequence(Player.WALK);
                    player.Status = Player.WALK;
                    if (selectedIndex == 0)
                    {
                        selectedIndex = (selectedIndex + 1) % _menuItems;
                    }
                }
            }
            if (newState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    player.SetFrameSequence(Player.WALK);
                    player.Status = Player.WALK;
                    if (selectedIndex == 1)
                    {
                        selectedIndex = (selectedIndex + 1) % _menuItems;
                    }
                }
            }
            if (newState.IsKeyDown(Keys.Left))
            {
                if (!oldState.IsKeyDown(Keys.Left))
                {
                    player.SetFrameSequence(Player.WALK);
                    player.SetTransform(SpriteEffects.None);
                    player.Status = Player.WALK;
                    if (selectedIndex == 2)
                    {
                        selectedIndex = (selectedIndex + 1) % _menuItems;
                    }
                }
            }
            if (newState.IsKeyDown(Keys.Right))
            {
                if (!oldState.IsKeyDown(Keys.Right))
                {
                    player.SetFrameSequence(Player.WALK);
                    player.SetTransform(SpriteEffects.FlipHorizontally);
                    player.Status = Player.WALK;
                    if (selectedIndex == 3)
                    {
                        selectedIndex = (selectedIndex + 1) % _menuItems;
                    }
                }
            }

            if (newState.IsKeyDown(Keys.A))
            {
                if (!oldState.IsKeyDown(Keys.A))
                {
                    if (player.Status != Player.ATK)
                    {
                        player.SetFrameSequence(Player.ATK);
                        player.Status = Player.ATK;
                        myGame.mediaControler.PlaySoundEffect(MediaControler.PLAYER.attack);
                        if (selectedIndex == 4)
                        {
                            selectedIndex = (selectedIndex + 1) % _menuItems;
                        }
                    }
                }
            }
            if (newState.IsKeyDown(Keys.S))
            {
                if (!oldState.IsKeyDown(Keys.S))
                {
                    if (player.Status != Player.SKILL)
                    {
                        player.SetFrameSequence(Player.SKILL);
                        player.Status = Player.SKILL;
                        //myGame.mediaControler.PlaySoundEffect(MediaControler.KUURYA2);
                        if (selectedIndex == 5)
                        {
                            selectedIndex = (selectedIndex + 1) % _menuItems;
                        }
                    }
                }
            }
            oldState = newState;
        }


        public override void Update(GameTime gameTime)
        {
            HandleKeyInput();
            if (player.dead > 0)
            {
                player.dead = 0;
                player.HP = 1000;
                player.Status = Player.STAND;
                player.SetFrameSequence(Player.STAND);
            }
            player.Update(gameTime);
            for (int i = 0; i < myButtons.Count; i++)
            {
                if (selectedIndex == i)
                {
                    myButtons[i].Visible = !myButtons[i].Visible;
                }
                else
                {
                    myButtons[i].Visible = true;
                }
            }
        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (background != null)
                background.Draw(sb, gameTime);
            for (int i = 0; i < myButtons.Count; i++)
            {
                myButtons[i].Draw(sb, gameTime);
            }
            player.Draw(sb, gameTime);
        }
    }
}
